﻿// =================================================================== 
// 项目说明
//====================================================================
// 陕西龙源  @Copy Right 2006-2008
// 文件： BS_DATASET_SQL.cs
// 项目名称：供电企业管理信息系统



// 创建时间：2010-03-17
// 负责人：
// ===================================================================
using System;
using System.Data;
using CommonLib.Model;
using CommonLib.IDAL;
using CommonLib.DALFactory;

namespace CommonLib.BLL
{
    /// <summary>
    ///   的BLL层



    /// </summary>
    public class BS_DATASET_SQL : BS_DATASET_SQLBLLBase
    {
		#region ----------变量定义----------
		#endregion
		
		#region ----------构造函数----------
		/// <summary>
		/// 默认构造函数



		/// </summary>
		public BS_DATASET_SQL()
		{
		}
		#endregion
		
		#region----------函数定义---------

        public static DataTable GetDataSetBySqlParam(int Dataset_id, string[,] ParamValue)
        {
            string strSql;
            DataSet ds = GetList("dataset_id=" + Dataset_id.ToString());
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                strSql = GetList("dataset_id=" + Dataset_id.ToString()).Tables[0].Rows[0]["dataset_sql"].ToString();
                strSql = ReplaceSQLParameter(Dataset_id, strSql, ParamValue);
                return _dal.SqlGetTable(strSql);
            }
            else
                return null;

        }


        public static DataTable GetDataSetBySqlParam(int Dataset_id, string strSql, string[,] ParamValue)
        {
            strSql = ReplaceSQLParameter(Dataset_id, strSql, ParamValue);
            return _dal.SqlGetTable(strSql);
        }

        public static DataTable SqlGetTable(string strSql)
        {
            return _dal.SqlGetTable(strSql);
        }

        /// <summary>
        /// 替换sql语句里面的参数，相同的参数名按同一个参数处理


        /// </summary>
        /// 
        /// <param name="strSql"></param>
        /// <param name="ParamValue">参数的值，相同的参数名按同一个参数处理</param>
        /// <returns></returns>
        public static string ReplaceSQLParameter(int Dataset_id, string strSql, string[,] ParamValue)
        {
            //int start,end,i;

            try
            {
                strSql = ReplaceSqlSysParam(strSql);
                if (ParamValue == null)
                    return strSql;
                for (int i = 0; i < ParamValue.GetLength(0); i++)
                {
                    strSql = strSql.Replace(ParamValue[i, 0], ParamValue[i, 1]);

                }

                //string[] strParamList = ParamValue.Split(',');
                //i=0;
                //while (true)
                //{
                //    start =strSql.IndexOf('[');
                //    end=strSql.IndexOf(']');
                //    if (start > 0 )
                //    {
                //        strParamName =strSql.Substring(start,end-start);
                //        strSql.Replace(strParamName,strParamList[i]);
                //    }
                //    else
                //        break;
                //    i++;
                //}
            }
            catch (System.Exception ex)
            {
                throw ex;
            }

            return strSql;


        }

        /// <summary>
        /// 替换sql语句里面的系统参数


        /// </summary>
        /// <param name="strSql">传入的字符串</param>
        /// <returns>返回替换后的字符串</returns>
        public static string ReplaceSqlSysParam(string strSql)
        {
            string CurrMonth = DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00");// Tools.GetCurrMonth();

            string NextMonth = (DateTime.Parse(CurrMonth.Substring(0, 4) + "-" + CurrMonth.Substring(4, 2) + "-" + "01")).AddMonths(1).ToString("yyyyMM");
            string LastMonth = (DateTime.Parse(CurrMonth.Substring(0, 4) + "-" + CurrMonth.Substring(4, 2) + "-" + "01")).AddMonths(-1).ToString("yyyyMM");
            string LastYearMonth = (DateTime.Parse(CurrMonth.Substring(0, 4) + "-" + CurrMonth.Substring(4, 2) + "-" + "01")).AddYears(-1).ToString("yyyyMM");

            string RetStr = strSql;
            //RetStr = RetStr.Replace("[发送对象号]", "");
            //RetStr = RetStr.Replace("[发送对象名]", "");
            RetStr = RetStr.Replace("[当前工作月份]", CurrMonth);
            //RetStr = RetStr.Replace("[当前操作员代码]", g_user_id);
            //RetStr = RetStr.Replace("[当前操作员名称]", g_user_name);
            RetStr = RetStr.Replace("[当前时间]", DateTime.Now.ToString("HH:mm:ss"));
            RetStr = RetStr.Replace("[当前年]", DateTime.Now.Year.ToString("0000"));
            RetStr = RetStr.Replace("[当前月]", DateTime.Now.Month.ToString("00"));
            RetStr = RetStr.Replace("[当前日]", DateTime.Now.Date.ToString("dd"));
            RetStr = RetStr.Replace("[去年]", DateTime.Now.AddYears(-1).Year.ToString("0000"));
            RetStr = RetStr.Replace("[上一个月份]", LastMonth);
            RetStr = RetStr.Replace("[下一个工作月份]", NextMonth);
            RetStr = RetStr.Replace("[今年一月份]", DateTime.Now.Year.ToString("0000") + "01");
            RetStr = RetStr.Replace("[今年十二月份]", DateTime.Now.Year.ToString("0000") + "12");
            RetStr = RetStr.Replace("[去年同期]", LastYearMonth);
            return RetStr;
        }

        //添加一个数据集记录
        public static int AddOneDataSet(CommonLib.Model.BS_DATASET_SQL oModel, DataTable paramDT)
        {
            MyTransaction sp = new MyTransaction();
            sp.BeginTransaction();
            try
            {
                int DataSetId = CommonLib.BLL.BS_DATASET_SQL.GetNewID();
                oModel.DATASET_ID = DataSetId;
                CommonLib.BLL.BS_DATASET_SQL.Add(sp, oModel);
                foreach (DataRow dr in paramDT.Rows)
                {
                    CommonLib.Model.BS_DATASET_PARAMETER oModalParam = new CommonLib.Model.BS_DATASET_PARAMETER();
                    oModalParam.DATA_CONTENT = dr["DATA_CONTENT"].ToString();
                    if (dr["DATA_SOURCE_STRING"].ToString() == "枚举")
                        oModalParam.DATA_SOURCE = 1;
                    else
                        oModalParam.DATA_SOURCE = 2;   
                    oModalParam.DATASET_ID = DataSetId;
                    if (dr["IS_SYSPARAMETER_STRING"].ToString() == "是")
                        oModalParam.IS_SYSPARAMETER = 1;
                    else
                        oModalParam.IS_SYSPARAMETER = 0;
          
                    oModalParam.NOTES = dr["NOTES"].ToString();
                    oModalParam.PARAMETER_DEFAULT = dr["PARAMETER_DEFAULT"].ToString();
                    oModalParam.PARAMETER_ID = dr["PARAMETER_ID"].ToString();
                    oModalParam.PARAMETER_NAME = dr["PARAMETER_NAME"].ToString();
                    if (dr["SELECT_TYPE_STRING"].ToString()=="下拉选项")
                        oModalParam.SELECT_TYPE = 1; 
                    else
                        oModalParam.SELECT_TYPE = 2; 
                    CommonLib.BLL.BS_DATASET_PARAMETER.Add(sp, oModalParam);
                }
                sp.CommitTran();
                return 0;
            }
            catch (System.Exception e)
            {
                sp.RollBackTran();
                throw (e);
                return -1;
            }
        }

        //修该一条数据集记录
        public static int UpdateOneDataSet(CommonLib.Model.BS_DATASET_SQL oModel, DataTable paramDT)
        {
            MyTransaction sp = new MyTransaction();
            sp.BeginTransaction();
            try
            {
                CommonLib.BLL.BS_DATASET_SQL.Update(sp,oModel);
                CommonLib.BLL.BS_DATASET_PARAMETER.DeleteFK(sp, " DATASET_ID = " + oModel.DATASET_ID);
                foreach (DataRow dr in paramDT.Rows)
                {
                    CommonLib.Model.BS_DATASET_PARAMETER oModalParam = new CommonLib.Model.BS_DATASET_PARAMETER();
                    oModalParam.DATA_CONTENT = dr["DATA_CONTENT"].ToString();
                    if (dr["DATA_SOURCE_STRING"].ToString() == "枚举")
                        oModalParam.DATA_SOURCE = 1;
                    else
                        oModalParam.DATA_SOURCE = 2;
                    oModalParam.DATASET_ID = oModel.DATASET_ID;
                    if (dr["IS_SYSPARAMETER_STRING"].ToString() == "是")
                        oModalParam.IS_SYSPARAMETER = 1;
                    else
                        oModalParam.IS_SYSPARAMETER = 0;

                    oModalParam.NOTES = dr["NOTES"].ToString();
                    oModalParam.PARAMETER_DEFAULT = dr["PARAMETER_DEFAULT"].ToString();
                    oModalParam.PARAMETER_ID = dr["PARAMETER_ID"].ToString();
                    oModalParam.PARAMETER_NAME = dr["PARAMETER_NAME"].ToString();
                    if (dr["SELECT_TYPE_STRING"].ToString() == "下拉选项")
                        oModalParam.SELECT_TYPE = 1;
                    else
                        oModalParam.SELECT_TYPE = 2;
                    CommonLib.BLL.BS_DATASET_PARAMETER.Add(sp, oModalParam);
                }
                sp.CommitTran();
                return 0;
            }
            catch (System.Exception e)
            {
                sp.RollBackTran();
                throw (e);
                return -1;
            }
        }

        //删除一条数据集记录
        public static int DeleteOneDataSet(int DataSetId)
        {
             MyTransaction sp = new MyTransaction();
            sp.BeginTransaction();
            try
            {
                CommonLib.BLL.BS_DATASET_PARAMETER.DeleteFK(sp, " DATASET_ID = " + DataSetId);
                CommonLib.BLL.BS_DATASET_SQL.Delete(sp, DataSetId);
                sp.CommitTran();
                return 0;
            }
            catch (System.Exception e)
            {
                sp.RollBackTran();
                throw (e);
                return -1;
            }

        }

		#endregion
	}
}
